package com.metadata_system.system.response;


import com.alibaba.fastjson.JSON;
import com.metadata_system.common.utils.MyUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class NodePropertiesItem {
    // 节点-属性-名称
    private String label;
    // 节点-属性-类型: boolean, string, number, object
    private String type;
    // 节点-属性-属性
    private String prop;
    // 节点-属性-单位
    private String unit;


    public static List<String> ParseMapToCondition(Map<String, Object> searchForm,String propertiesString) {

        // 构造node的属性map
        List<NodePropertiesItem> nodePropertiesItems = JSON.parseArray(propertiesString, NodePropertiesItem.class);

        Map<String,NodePropertiesItem> nodePropertiesItemMap = new HashMap<>();
        for (NodePropertiesItem nodePropertiesItem : nodePropertiesItems) {
            nodePropertiesItemMap.put(nodePropertiesItem.getProp(),nodePropertiesItem);
        }

        // 构造查询条件
        List<String> conditions = new java.util.ArrayList<>();
        conditions.add("1=1");
        for (Map.Entry<String, Object> entry : searchForm.entrySet()) {
            NodePropertiesItem currItem = nodePropertiesItemMap.get(entry.getKey());
            if (currItem != null) {
//                if (currItem.getType().equals("boolean")) {
//                    conditions.add(entry.getKey() + " = " + entry.getValue());
//                } else {
//                    conditions.add(entry.getKey() + " LIKE \"%" + entry.getValue() + "%\"");
//                }
                if (currItem.getType().equals("number")) {
                    String[] split = entry.getValue().toString().split(",");
                    if(split.length==2){
                        if(!StringUtils.isBlank(split[0])){
                            conditions.add(entry.getKey() + " >= " + split[0]);
                        }
                        if(!StringUtils.isBlank(split[1])){
                            conditions.add(entry.getKey() + " <= " + split[1]);
                        }
                    }
                } else {
                    conditions.add(entry.getKey() + " LIKE \"%" + entry.getValue() + "%\"");
                }
            }
        }
        return conditions;
    }
}
